<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
             height: 1000px;
        }

        li {
            list-style: none;
        }

        img {
            width: 100%;
            height: 120px;
        }

        .focus {
            position: relative;
            overflow: hidden;
        }

        .focus ul {
            width: 500%;
            overflow: hidden;
            margin-left: -100%;
            z-index: 999;
        }

        .focus ul li {
            float: left;
            width: 20%;
        }

        .focus ol {
            position: absolute;
            bottom: 5px;
            right: 5px;
        }

        .focus ol li {
            display: inline-block;
            width: 5px;
            height: 5px;
            background-color: #fff;
            border-radius: 2px;
            transition: all .3s;
        }

        .focus ol li.current {
            width: 10px;
        }

        button {
            display: none;
            position: absolute;
            bottom: 10%;
            right: 10px;
            
        }
    </style>
</head>

<body>
    <div class="focus">
        <ul>
            <li><img src="images/focus_3.jfif" alt=""></li>
            <li><img src="images/focus_1.webp" alt=""></li>
            <li><img src="images/focus_2.jfif" alt=""></li>
            <li><img src="images/focus_3.jfif" alt=""></li>
            <li><img src="images/focus_1.webp" alt=""></li>
        </ul>
        <ol>
            <li class="current"></li>
            <li></li>
            <li></li>
        </ol>
    </div>
    <button>点击</button>
    <script>
        window.addEventListener('load', function () {
            var focus = document.querySelector('.focus');
            var ul = focus.children[0];
            var ol = focus.children[1];
            // console.log(ul);
            var w = focus.offsetWidth;
            var index = 0;
            var timer = setInterval(function () {
                index++;
                var translate_x = -index * w;
                ul.style.transition = 'all .3s';
                ul.style.transform = 'translateX(' + translate_x + 'px)'
            }, 2000);
            //等着我们过渡完之后，再去判断 监听过度完成的事件 transitionend
            ul.addEventListener('transitionend', function () {
                // console.log(1);
                //无缝滚动
                if (index >= 3) {
                    index = 0;
                    //去掉过渡效果 ，这样让我们的ul 快速的跳到目标位置
                    ul.style.transition = 'none';
                    //利用最新的索引号乘以宽度 去滚动图片
                    var translate_x = -index * w;
                    ul.style.transform = 'translateX(' + translate_x + 'px)'
                } else if (index < 0) {
                    index = 2;
                    ul.style.transition = 'none';
                    //利用最新的索引号乘以宽度 去滚动图片
                    var translate_x = -index * w;
                    ul.style.transform = 'translateX(' + translate_x + 'px)'
                }
                //小圆点跟随变化 
                //把ol里面的li带有current的类名选出来去掉类名 remove
                ol.querySelector('.current').classList.remove('current');
                //让当前索引号的li 加上 current add添加
                ol.children[index].classList.add('current');
            });
            var startX = 0;
            var moveX = 0;
            var flag = false;
            ul.addEventListener('touchstart', function (e) {
                startX = e.targetTouches[0].pageX;
                clearInterval(timer);
            })
            //移动手指 touchmove ：计算手指的滑动距离 并且移动盒子
            ul.addEventListener('touchmove', function (e) {
                //计算移动距离
                // console.log(1);
                moveX = e.targetTouches[0].pageX - startX;
                //移动盒子  盒子原来的位置 + 手指移动的距离
                var translate_x = -index * w + moveX;
                //手指拖动的时候 不需要动画效果所以要取消过渡效果
                ul.style.transition = 'none';
                ul.style.transform = 'translateX(' + translate_x + 'px)';
                flag = true; // 如果用户手指移动过我们再去判断否则不做判断效果
                e.preventDefault(); //阻止屏幕默认滚动
            });
            //手指离开 根据移动距离去判断是回弹还是播放上一张下一张
            ul.addEventListener('touchend', function (e) {
                if (flag) {
                    if (Math.abs(moveX) > 50) {
                        if (moveX > 0) {
                            //如果是右滑 就是 播放上一张 moveX 是正值
                            index--;
                        } else {
                            //如果是左滑 就是 播放下一张 moveX 是负值
                            index++;
                        }
                        var translate_x = -index * w;
                        ul.style.transition = 'all .3s';
                        ul.style.transform = 'translateX(' + translate_x + 'px)';
                    } else {
                        var translate_x = -index * w;
                        ul.style.transition = 'all .1s';
                        ul.style.transform = 'translateX(' + translate_x + 'px)';
                    }

                }
                //开启时先关闭定时器
                clearInterval(timer);
                //开启定时器
                timer = setInterval(function () {
                    index++;
                    var translate_x = -index * w;
                    ul.style.transition = 'all .3s';
                    ul.style.transform = 'translateX(' + translate_x + 'px)'
                }, 2000);

            });
            var btn = document.querySelector('button');
            window.addEventListener('scroll', function () {
                if (window.pageYOffset >= 200) {
                    btn.style.display = 'block';
                } else {
                    btn.style.display = 'none';
                }
            });
            btn.addEventListener('click', function () {
                window.scroll(0, 0);
            })

        })
    </script>
</body>

</html>